<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Ttt extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->config->load('sns_openapi');
        $url = APPPATH . "libraries" . "/sinaweibo/saetv2.ex.class.php";
        require_once $url;
        $tsianconfig = $this->config->item('tsian');
        $this->tsianconfig = $tsianconfig;
        $this->o = new SaeTOAuthV2($tsianconfig["appkey"], $tsianconfig["secret"]);
        $this->code_url = $this->o->getAuthorizeURL($tsianconfig["call_back"]);

        if (isset($_SESSION['token']['access_token'])) {
            $this->c = new SaeTClientV2($tsianconfig["appkey"], $tsianconfig["secret"], $_SESSION['token']['access_token']);
        }
    }

    public function index() {
        $code_url = $this->code_url;
        redirect($code_url);
    }

    public function tsina_access_token() {
        $tsianconfig = $this->tsianconfig;
        $keys = array();
        $keys['code'] = $_GET['code'];
        $keys['redirect_uri'] = $tsianconfig["call_back"];
        // print_r($this->o);
        echo $token = $this->o->getAccessToken('code', $keys);
        print_r($token);

        if ($token) {
            $_SESSION['token'] = $token;
            setcookie('weibojs_' . $this->o->client_id, http_build_query($token));
        }
        print_r($_SESSION);
        redirect("ttt/tsina_func_call");
        // exit;
    }

    public function tsina_func_call() {
        $ms = $this->c->home_timeline(); // done
        $uid_get = $this->c->get_uid();
        $uid = $uid_get['uid'];
        $user_message = $this->c->show_user_by_id($uid); //根据ID获取用户等基本信息
        print_r($user_message);
        $ret = $this->c->upload("这是一条测试，:)!for the1 future@", "http://www.mhtml5.com/wp-content/uploads/2011/09/facebook-logo.jpg"); //发送微博
        print_r($ret);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */